//
//  WALocation.m
//  WeatherApp
//
//  Created by Yuliya Grasevych on 23.07.14.
//  Copyright (c) 2014 Yuliya Grasevych. All rights reserved.
//

#import "WALocation.h"
#import "WAYahooInfoProvider.h"

NSString *const kWALocationDidUpdate = @"kWALocationDidUpdate";

@implementation WALocation

@synthesize hemisphere = _hemisphere;

-(id)initWithLocation:(CLLocation *)location
{
    self = [super init];
    if (self) {
        
        _location = location;
        if (_location) {
            
            CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
            [geoCoder reverseGeocodeLocation:_location completionHandler:^(NSArray *placemarks, NSError *error) {
                
                CLPlacemark *placemark = [placemarks firstObject];
                self.city = placemark.locality;
                self.country = placemark.country;
                self.district = placemark.administrativeArea;
                
                // request woeid
                
                
                NSDictionary *params = @{@"q": [NSString stringWithFormat:@"select woeid from geo.placefinder where text=\"%f,%f\" and gflags=\"R\"", _location.coordinate.latitude, _location.coordinate.longitude],
                                         @"format": @"json"};
                
                [[WAYahooInfoProvider sharedInstance] yahooRequestWithStatement:params
                                                              completionHandler:^(NSError *error, NSDictionary *jsonData) {
                                                                  if (!error) {
                                                                      
                                                                      NSDictionary *query = [jsonData objectForKey:@"query"];
                                                                      NSDictionary *results = [query objectForKey:@"results"];
                                                                      NSDictionary *Result = [results objectForKey:@"Result"];
                                                                      self.woeid = [Result objectForKey:@"woeid"];
                                                                  }
                                                                  [[NSNotificationCenter defaultCenter] postNotificationName:kWALocationDidUpdate object:self];
                                                              }];
                
            }];
        }
    }
    return self;
}

-(id)initWithWoeid:(NSString *)woeid
{
    self = [super init];
    if (self) {
        
        _woeid = woeid;
    }
    return self;
}

-(WAHemisphere)hemisphere
{
    if (!_hemisphere) {
        
        _hemisphere = (self.location.coordinate.latitude >= 0) ? WANorthernHemisphere : WASouthernHemisphere;
    }
    return _hemisphere;
}

-(NSString *)stringRepresentation
{
    NSMutableString *result = [NSMutableString string];
    if (self.city) {
        
        [result appendString:self.city];
        [result appendString:@", "];
    }
    if (self.district) {
        
        if (![self.district isEqualToString:self.city]) {
            
            [result appendString:self.district];
            [result appendString:@", "];
        }
    }
    if (self.country) {
        
        [result appendString:self.country];
    }
    
    return result;
}

-(BOOL)isEqualToLocation:(WALocation *)location
{
    BOOL result = NO;
    if ([self.woeid isEqualToString:location.woeid]) {
        
        result = YES;
    }
    return result;
}

-(id)copyWithZone:(NSZone *)zone
{
    WALocation *copy = [[[self class] allocWithZone:zone] init];
    copy.location = self.location;
    copy.city = self.city;
    copy.country = self.country;
    copy.district = self.district;
    copy.woeid = self.woeid;
    return copy;

}
-(NSUInteger) hash;
{
    return [_stringRepresentation hash];
}

-(BOOL) isEqual:(id)other;
{
    if([other isKindOfClass:[WALocation class]])
        return [[self stringRepresentation] isEqualToString:((WALocation*)other).stringRepresentation];
    else
        return NO;
}
@end
